const db = require("../db/index.js");
// 引入brycpt 加密中间件
const bcrypt = require("bcryptjs");
// 导入crypto模块 生成uuid
const crypto = require("crypto");
// 导入fs处理文件
const fs = require("fs");

// 上传轮播图 需要两个参数
exports.uploadSwiper = (req, res) => {
  // console.log(req.body);
  let str = "";
  for (let i = 0; i < 7; i++) {
    str += req.body[i];
  }
  // console.log(req.body.str);
  //   res.send(req.body);
  // let str = req.body.str;
  let oldName = req.files[0].filename;
  let newName = Buffer.from(req.files[0].originalname, "latin1").toString(
    "utf8"
  );
  fs.renameSync("./public/upload/" + oldName, "./public/upload/" + newName);
  const sql = "update setting set set_value = ? where set_name = ?";
  db.query(
    sql,
    [`http://127.0.0.1:3007/upload/${newName}`, str],
    (err, result) => {
      if (err) return res.cc(err);
      res.send({
        status: 0,
        message: "上传轮播图成功",
      });
    }
  );
};

/**
 * 获取所有轮播图
 */
exports.getAllSwiper = (req, res) => {
  const sql = "select * from setting where set_name like 'swiper%'";
  db.query(sql, (err, result) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      message: "获取轮播图成功",
      data: result,
    });
  });
};

/**
 * 获取公司名称
 */
exports.getCompanyName = (req, res) => {
  const sql = "select * from setting where set_name='公司名称' ";
  db.query(sql, (err, result) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      data: result,
    });
  });
};

/**
 * 获取公司介绍详情
 */
exports.getCompanyIntroduceDetail = (req, res) => {
  const sql = "select * from setting where set_name='公司介绍' ";
  db.query(sql, (err, result) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      data: result[0].set_text,
    });
  });
};

/**
 * 修改公司名称
 * @data set_value
 */
exports.changeCompanyName = (req, res) => {
  const { set_value } = req.body;
  console.log(set_value);
  const sql = "update setting set set_value = ? where set_name ='公司名称' ";
  db.query(sql, set_value, (err, result) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      message: "修改公司名称成功",
    });
  });
};

/**
 * 修改公司相关信息
 * @data set_value set_name
 */
exports.changeCompanyIntroduce = (req, res) => {
  const { set_value, set_name } = req.body;
  const sql = "update setting set set_value = ? where set_name = ? ";
  db.query(sql, [set_value, set_name], (err, result) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      message: "修改成功",
    });
  });
};

/**
 * 获取所有公司相关信息
 */
exports.getAllCompanyIntroduce = (req, res) => {
  const sql = "select * from setting where set_name like '公司%' ";
  db.query(sql, (err, result) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      message: "获取成功",
      data: result,
    });
  });
};

// 获取公司介绍 参数 set_name
exports.getCompanyIntroduce = (req, res) => {
  const sql = "select * from setting where set_name = ?";
  db.query(sql, req.body.set_name, (err, result) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      message: "获取成功",
      data: result,
    });
  });
};
